/*
 * prec.c
 *
 *  Created on: 29-05-2012
 *      Author: Mati
 */

#include "prec.h"

extern uint16_t DesiredTemperature;
extern xTimerHandle xModesTimer;

/*
 * IN1 - gain control in measuring circuit; IN1=0 (prec300), IN1=0 (prec20)
 * IN2 - feedback loop (opamp gain); IN2=0 (prec300), IN2=0 (prec20)
 */
__INLINE void ConfigPrecControl()
{
	LPC_GPIO1->FIODIR  |=  0x01900000;				//set as outputs: P1.23=IN1; P1.24=IN2; P1.20=E/S;
	LPC_PINCON->PINSEL3 &= ~(3U<<8)  &
						   ~(3U<<14) &
	   	   	   	   	   	   ~(3U<<16) ;
}
__INLINE void SetPrec20()
{
	LPC_GPIO1->FIOPIN |= 0x01800000;
}
__INLINE void SetPrec300()
{
	//LPC_GPIO1->FIOPIN &= ~0x01800000;
	LPC_GPIO1->FIOPIN ^= 0x01800000;
}

/* Funkcja podaje napiecie na E/S OPA548 i wlacza sterowanie napieciem*/
__INLINE void TurnOnVoltage()
{
	LPC_GPIO1->FIOPIN |= 0x00100000;
	//LPC_GPIO1->FIOPIN ^= 0x00100000;
}

__INLINE void TurnOffVoltage()
{
	LPC_GPIO1->FIOPIN &= ~0x00100000;
}

uint16_t GetTemperature()
{
//	/PVal.
}

void SetPeriodAndTemperature(uint16_t temp, uint16_t period)
{
	/* set temperature */
	DesiredTemperature = temp;

    /* xTimer is not active, change its period to 500ms.  This will also
    cause the timer to start.  Block for a maximum of 10 ticks if the
    change period command cannot immediately be sent to the timer
    command queue. */
    if( pdTRUE == xTimerStop(xModesTimer, 0) )
    {
   	 if( xTimerChangePeriod( xModesTimer, (period * 60000), 10 ) == pdPASS )	//period w minutach -> przeliczenie na ticki 1ms
    	 {
        	 //printf("Period changed: %d [min]\n\r", period);
        	 //xTimerStart(xModesTimer, 0);
    	 }
    	 else
    	 {
    		 //printf("Couldn't change the period!\n\r");
    		 /* The command could not be sent, even after waiting for 10 ticks
        	 to pass.  Take appropriate action here. */
    	 }
    }
}
